
//这个reducer 只负责管理 arr 这个数组


function arrReducer( state = [], action = {type:'',payload:null} ){
    //对state进行深拷贝
    var newstate = JSON.parse(JSON.stringify(state));

    switch( action.type ){

        case 'LIST':
            newstate= action.payload
            return newstate;
        case 'ADD':
            newstate.push(action.payload)
            return newstate;
        case 'DELETE':
            var index=newstate.findIndex((item) => {
                return item == action.payload
            })
            if(index!=-1){
                newstate.slice(index,1)
            }
            return newstate;
        case 'UPDATE':
            var index=newstate.findIndex((item) => {
                return item.id == action.payload.id
            })
            if(index!=-1){
                newstate.slice(index,1)
                newstate.splice(index, 1)
            }
            return newstate;
        default: return state;
    }
}

export default arrReducer;